package com.dayaokeji.rhythmschoolstudent.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialog;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.dayaokeji.rhythmschoolstudent.R;
import com.yanzhenjie.permission.RequestExecutor;

import java.lang.ref.SoftReference;

/**
 * Created by Gorden on 2017/10/9.
 * dialog 工具类
 */

public class DialogUtils {

    /**
     * AppCompatDialog 固定80%宽度dialog
     * @param context
     * @param layoutResId
     * @return
     */
    @Nullable
    public static AppCompatDialog getAppCompatDialog(Activity context, @LayoutRes int layoutResId) {

        SoftReference<Activity> contextSoftReference = new SoftReference<Activity>(context);
        context = contextSoftReference.get();
        if (context != null) {
            final AppCompatDialog appCompatDialog = new AppCompatDialog(context);
            appCompatDialog.setContentView(layoutResId);
            if (!context.isFinishing()) {
                appCompatDialog.show();
                Display defaultDisplay = appCompatDialog.getWindow().getWindowManager().getDefaultDisplay();
                DisplayMetrics displayMetrics = new DisplayMetrics();
                defaultDisplay.getMetrics(displayMetrics);
                int width = (int) (displayMetrics.widthPixels * 0.8);
                WindowManager.LayoutParams attributes = appCompatDialog.getWindow().getAttributes();
                attributes.width = width;
                appCompatDialog.getWindow().setAttributes(attributes);
            }
            return appCompatDialog;
        }else{
            return null;
        }
    }

    /**
     * 常规取消确定dialog
     * @param context
     * @param title
     * @param message
     * @param onClickListener
     * @return
     */
    @Nullable
    public static AlertDialog getAlertDialog(Activity context, String title, String message, final DialogInterface.OnClickListener onClickListener) {
        SoftReference<Activity> contextSoftReference = new SoftReference<Activity>(context);
        context = contextSoftReference.get();
        if (context != null) {
            final AlertDialog.Builder dialogBuilder =  new android.support.v7.app.AlertDialog.Builder(context);
            dialogBuilder.setTitle(title);
            dialogBuilder.setMessage(message);
            dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    if (onClickListener != null) {
                        onClickListener.onClick(dialogInterface,i);
                    }
                }
            });
            if (context.isFinishing()) {
                return null;
            }
            final android.support.v7.app.AlertDialog dialog = dialogBuilder.show();
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.gray));
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.warring_red));
            return dialog;
        }else{
            return null;
        }
    }


    /**
     * 权限拒绝提示dialog
     */
    public static void showPermissionDenyDialog(Activity context, int msgResId,RequestExecutor executor,DialogInterface.OnClickListener onCancelListener) {
        showPermissionDenyDialog(context,context.getResources().getString(msgResId),executor);
    }

    /**
     * 权限拒绝提示dialog
     */
    public static void showPermissionDenyDialog(Activity context, String msg, final RequestExecutor executor) {
        if (!context.isFinishing()) {
            final android.support.v7.app.AlertDialog.Builder dialogBuilder = new android.support.v7.app.AlertDialog.Builder(context);
            dialogBuilder.setTitle("申请权限失败");
            dialogBuilder.setMessage(msg);
            dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    executor.cancel();
                    dialogInterface.dismiss();
                }
            });
            dialogBuilder.setPositiveButton("继续授权", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    executor.execute();
                    dialogInterface.dismiss();
                }
            });
            AlertDialog dialog = dialogBuilder.create();
            if (!context.isFinishing() && !dialog.isShowing()) {
                dialog.show();
            }
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.colorPrimary));
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.warring_red));
        }
    }
}
